"""
# -*- coding: utf-8 -*-
# @Time    : 2023/10/16 15:02
# @Author  : 王摇摆
# @FileName: file1.py
# @Software: PyCharm
# @Blog    ：https://blog.csdn.net/weixin_44943389?type=blog
"""

# 导入必要的库
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC

# 读取数据
data = pd.read_csv('../dataset/train.csv')
test_data = pd.read_csv('../dataset/test.csv')
print('[1. 数据集加载完毕]')

# 分离特征和目标变量
X = data.drop(columns=['id', 'target'])
y = data['target']

test_X = test_data.drop(columns='id')

# 划分训练集和测试集
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

print('[2. 数据集预处理完成]')

# 初始化并训练SVM模型
knn_classifier = KNeighborsClassifier(n_neighbors=5)  # 修改此处，选择合适的K值
knn_classifier.fit(X, y=y)
print('[3. KNN模型训练完成]')

# 预测
y_pred = knn_classifier.predict(X=test_X)
print('[4. SVM模型预测完毕]\n')

# 预测结果输出
pd.DataFrame({'id': test_data['id'], 'target': y_pred}).to_csv('../result/SVM.csv', index=None)
print('[5. 预测结果已输出为CSV文件]')


